import Vue from 'vue'
import api from './index'
// axios
import request from '@/utils/request'
import { Toast } from 'vant'
import {toLogin} from "./login";

import UnitechsJs from 'unitechs-js'
Vue.use(UnitechsJs, {})


export function login() {
  const userInfo = {
    username: 'guorong',
    password: 'Guorong@123456',
    checkcode: 'jcr9'
  }
  const username = userInfo.username.trim()
  const captcha = userInfo.checkcode.trim()
  // import Cookies from 'js-cookie'

  let encPass = UnitechsJs.utils.md5(userInfo.password)
  const date = new Date()
  const timestamp =
    '' +
    (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) +
    (date.getMinutes() < 10
      ? '0' + date.getMinutes()
      : date.getMinutes()) +
    (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds())
  encPass = timestamp + UnitechsJs.utils.md5(userInfo.password)
  const params = {
    loginName: username,
    password: encPass,
    captcha: captcha
  }
  let url = ''
  /*if (process.env.NODE_ENV === 'production') {
    url = 'plat/auth/token'
  } else {
    url = 'plat/auth/token'
  }*/

  toLogin(params).then(data => {
    UnitechsJs.utils.setCookie('zy_token', data.token)
    UnitechsJs.utils.setCookie('zy-user-id', data.userId)
    UnitechsJs.utils.setCookie('zy-netuser-id', params.loginName)

    window.sessionStorage.setItem('tempFlag', '1')
    window.sessionStorage.setItem('zy_token', data.token)
    window.sessionStorage.setItem('zy-user-id', data.userId)
    window.sessionStorage.setItem('zy-netuser-id', params.loginName)
    if (data.blocks) {
      window.sessionStorage.setItem(
        'blacklist',
        JSON.stringify({ blocks: data.blocks || [] })
      )
    } else {
      window.sessionStorage.removeItem('blacklist')
    }
  })
}

// 用户信息 post 方法
export function getUserInfo(data) {
  return request({
    url: api.UserInfo,
    method: 'post',
    data,
    hideloading: true
  })
}

// 用户名称 get 方法
export function getUserName(params) {
  return request({
    url: api.UserName,
    method: 'get',
    params,
    hideloading: true
  })
}
